package com.component;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.SmartFactoryBean;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author fanxiao  2022/3/30
 * @since 1.0.0
 */
public class CandidatesFactoryBean<T> implements SmartFactoryBean<T>, InitializingBean {

	private Class<T> serviceInterface;

	public CandidatesFactoryBean() {

	}

	public CandidatesFactoryBean(Class<T> serviceInterface){
		this.serviceInterface = serviceInterface;
	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}

	@Override
	public T getObject() throws Exception {
		CandidatesProxyFactory proxyFactory = new CandidatesProxyFactory(serviceInterface);
		return (T) proxyFactory.newInstance();
	}

	@Override
	public Class<?> getObjectType() {
		return serviceInterface.getClass();
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

	@Override
	public boolean isPrototype() {
		return false;
	}

	@Override
	public boolean isEagerInit() {
		return true;
	}
}